package com.game.worm.mobile.view.list.taglist;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dx.as.ToolBarOptions;
import com.dx.as.UI;
import com.dx.as.http.IHttpListener;
import com.game.worm.mobile.R;
import com.game.worm.mobile.bean.GameBean;
import com.game.worm.mobile.view.home.adpater.GamedAdpater;
import com.game.worm.mobile.view.http.HttpHelper;

import java.util.List;

import static com.game.worm.mobile.view.GameExtras.EXTRAS_GAME_BY_HOT;
import static com.game.worm.mobile.view.GameExtras.EXTRAS_GAME_BY_TAG;
import static com.game.worm.mobile.view.GameExtras.EXTRAS_GAME_BY_TYPE;
import static com.game.worm.mobile.view.GameExtras.EXTRAS_GAME_BY_WHAT;
import static com.game.worm.mobile.view.GameExtras.EXTRAS_GAME_TAG_ID;
import static com.game.worm.mobile.view.GameExtras.EXTRAS_GAME_TAG_NAME;

/**
 * Created by d_xxiang on 2018/5/22.
 */

public class GameListActivity extends UI{
    private TabLayout tabLayout;
    private RecyclerView recyclerview;
    private GamedAdpater gamedAdpater;
    private String tag;
    private int type = EXTRAS_GAME_BY_TAG;
    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_tag_game_list);
        initData();
        initView();
        doHttp();
    }

    private void initData(){
        tag = getIntent().getStringExtra(EXTRAS_GAME_TAG_ID);
        String tagName = getIntent().getStringExtra(EXTRAS_GAME_TAG_NAME);
        type = getIntent().getIntExtra(EXTRAS_GAME_BY_WHAT,EXTRAS_GAME_BY_TAG);
        setToolBar(tagName);
    }

    private void setToolBar(String title){
        ToolBarOptions toolBarOptions = new ToolBarOptions();
        toolBarOptions.titleString = title;
        setToolbar(toolBarOptions);
    }

    private void doHttp(){
        if (type == EXTRAS_GAME_BY_TAG){
            HttpHelper.getTagGameList(tag,1,1,iHttpListener);
        }else if(type == EXTRAS_GAME_BY_TYPE){
            HttpHelper.getTypeGameList(tag,1,1,iHttpListener);
        }else if (type == EXTRAS_GAME_BY_HOT){
            HttpHelper.getHostList(1,iHttpListener);
        }
    }

    private void initView(){
        tabLayout = findViewById(R.id.tabs);
        if (type != EXTRAS_GAME_BY_HOT){

            String[] titles = getResources().getStringArray(R.array.tag_game_tab);
            for (int i = 0; i < titles.length; i++) {
                tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
            }
            tabLayout.addOnTabSelectedListener(onTabSelectedListener);
        }else tabLayout.setVisibility(View.GONE);

        recyclerview = findView(R.id.recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(linearLayoutManager);
        gamedAdpater = new GamedAdpater(this);
        recyclerview.setAdapter(gamedAdpater);
    }

    private TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    };

    private IHttpListener iHttpListener = new IHttpListener() {
        @Override
        public void onFilure(int code, String errorMsg) {
        }

        @Override
        public void onResponse(Object response, int flg) {
            if (response != null){
                gamedAdpater.addData((List<GameBean>)response);
                gamedAdpater.notifyDataSetChanged();
            }
        }

        @Override
        public void onFailure(Throwable t) {
        }
    };

}
